package com.example.demo.mvvm.ui.vm.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;

import com.example.demo.mvvm.adapter.MainViewPagerAdapter;
import com.example.demo.mvvm.bean.ArticleBean;
import com.example.demo.mvvm.bean.KnowBean;
import com.example.demo.mvvm.databinding.ActivityChildrenBinding;
import com.example.demo.mvvm.ui.base.BaseViewVM;
import com.example.demo.mvvm.ui.fragment.ChildrenFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by littlehu on 2018/5/18.
 */

public class ChildrenActivityVm  extends BaseViewVM{

    private FragmentManager manager ;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> tagLists = new ArrayList<>();
    private List<Integer> idLists = new ArrayList<>();
    private MainViewPagerAdapter adapter ;

    public ChildrenActivityVm(ActivityChildrenBinding binding , Intent intent , FragmentManager manager , ActionBar actionBar){
        this.manager = manager;
        tagLists.clear();
        idLists.clear();
        fragments.clear();
        if(intent.hasExtra("knowBean")){
            KnowBean knowBean = (KnowBean) intent.getSerializableExtra("knowBean");
            actionBar.setTitle(knowBean.getName());
            for(KnowBean.ChildrenBean childrenBean : knowBean.getChildren()){
                tagLists.add(childrenBean.getName());
                idLists.add(childrenBean.getId());
            }
        }

        if(intent.hasExtra("articleBean")){
            ArticleBean bean = (ArticleBean) intent.getSerializableExtra("articleBean");
            actionBar.setTitle(bean.getChapterName());
            tagLists.add(bean.getChapterName());
            idLists.add(bean.getChapterId());
        }
        for(Integer t : idLists){
            fragments.add(ChildrenFragment.newInstance(t));
        }
        adapter = new MainViewPagerAdapter(manager ,tagLists.toArray(new String[tagLists.size()]) , fragments);

        binding.viewPager.setAdapter(adapter);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
    }
}
